{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3. Longest Substring Without Repeating Characters**  <font color=#f0ad4e size=3>_medium_</font>\n",
    "\n",
    "Given a string, find the length of the longest substring without repeating characters.\n",
    "\n",
    "**Example 1**\n",
    "```\n",
    "Input: \"abcabcbb\"\n",
    "Output: 3\n",
    "Explanation: The answer is \"abc\", with the length of 3. \n",
    "```\n",
    "\n",
    "**Example 2**\n",
    "```\n",
    "Input: \"bbbbb\"\n",
    "Output: 1\n",
    "Explanation: The answer is \"b\", with the length of 1. \n",
    "```\n",
    "\n",
    "**Example 3**\n",
    "\n",
    "```\n",
    "Input: \"pwwkew\"\n",
    "Output: 3\n",
    "Explanation: The answer is \"wke\", with the length of 3.\n",
    "             Note that the answer must be a substring, \"pwke\" is a subsequence and not a substring.\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def lengthOfLongestSubstring(s):\n",
    "    \"\"\"\n",
    "    Input: \"abcabcbb\"\n",
    "    Output: 3\n",
    "    Explanation: The answer is \"abc\", with the length of 3.\n",
    "    \"\"\"\n",
    "    # sets = set()\n",
    "    # len = 0\n",
    "    # for i, char in enumerate(s):\n",
    "    #     for j in range(i, s.__len__()):\n",
    "    #         if sets.__contains__(s[j]):\n",
    "    #             if sets.__len__() > len:\n",
    "    #                 len = sets.__len__()\n",
    "    #             sets.clear()\n",
    "    #             break\n",
    "    #         else: sets.add(s[j])\n",
    "    #\n",
    "    # return len\n",
    "\n",
    "    ########## Sliding Window #########\n",
    "    sets = set()\n",
    "    i,j = 0,0\n",
    "    n = s.__len__()\n",
    "    len = 0\n",
    "    while i < n and j < n :\n",
    "        if not sets.__contains__(s[j]):\n",
    "            sets.add(s[j])\n",
    "            j += 1\n",
    "        else:\n",
    "            if sets.__len__() > len:\n",
    "                # print sets\n",
    "                len = sets.__len__()\n",
    "            sets.remove(s[i])\n",
    "            i += 1\n",
    "    return len\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(lengthOfLongestSubstring(\"abcabcbb\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
